exports.GAPIConnectHandler = Core.Class.subclass({

	classname: 'GAPIConnectHandler',

	initialize: function() 
	{
		//GAConfigurations.setGapiConfiguration("ngTJ", 1.0,"ngJurassic","GameView Studio", "https://ngtapjurassicservice.appspot.com/", "./Content/db", "./Content/textures/Stores");
//		GAGameLoader.setDelegate(this);
		
		this._readCount = 0;
		this._userLevel = 1;
		this._experience = 0;
		this._offerCoins = 0;
		this._offerBucks = 0;
		this._purchasedCoins = 0;
		this._purchasedBucks = 0;
		this._zooArray = null;
		this.gameStateType = null;
		this.fileNames = null;

		this._startGameConnect();
		
	},
	_startGameConnect:function(){
		
		//this.fileNames = ["User"];
		//GAConfigurations.setGapiConfiguration("ngTJ", 1.1,"ngJurassic","GameView Studio", "https://ngtapjurassicservice.appspot.com/", "./Content/db", "./Content/textures/Stores",this.fileNames);
		
		this.fileNames = ["User"];
		GAConfigurations.setGapiConfiguration("ngMM", 1.0,"ngMonsterMall","Gaminations Inc.", "http://xray483.startdedicated.com/", "./Content/db", "./Content/textures/Stores",this.fileNames, this.doneConfigurations.bind(this));
		
	},
	
	doneConfigurations: function()
	{
		this.startLoading();
	},
	
	startLoading: function()
	{
		console.log("Zuluuu:::: startConnect");
		
		this.getLocalXp(); 
		
		//GAGameLoader.startConnect(this._xp);
	},
	
	getLocalXp: function()
	{
		var sourcePath = GAConfigurations.getGameStateDBPath()+"/";
		var fileName = sourcePath+GAConfigurations.getUserId()+"/User.json";
				
		Storage.FileSystem.readFile(fileName,this._readGameStateForUserCb.bind(this));
	},
	
	_readGameStateForUserCb: function(error, contents)
	{
		var xp = "0";
		if(!error){
			var data = eval('('+ contents +')');
			xp = data.xp;
		}
 		GAGameLoader.instantiate();
 		GAGameLoader.setDelegate(this);
 		GAGameLoader.startConnect(xp); 
	},

	readDataBase:function(path,readDBSuccess) {
												 
		//console.log('>> readDataBase '+path);
		this.readDBSuccess = readDBSuccess;
		var name = 'User';
		Storage.FileSystem.readFile(path+'/'+name+'.json',this._dbReadCallback.bind(this));
	},
	
	_dbReadCallback: function (error,contents){
		//console.log('>> _dbReadCallback '+error);
		if(!error){
			var data = eval('('+ contents +')');
			this._userLevel = data.currentLevel;
			this._experience = data.exp;
			this._offerCoins = data.offerCoins;
			this._offerBucks = data.offerBucks;
			this._purchasedCoins = data.purchasedCoins;
			this._purchasedBucks = data.purchasedBucks;
		}
		///zaki local
		
		//this.readDBSuccess(this._userLevel,this._experience,this._purchasedCoins,this._purchasedBucks,this._offerCoins,this._offerBucks);
		this.onSuccess();
	},
		
	onSuccess: function () {
		//console.log(">> ===--- MAIN::onSuccess() ---===");
		
		//this.initializeBreedingAttributesForHierarchyModel();
		GameCentralHandler.onConnectSuccess();
		
	},
	
	onFailure: function(errorCode) 
	{
		var alertBox = UIFactory.getAlertBox(Core.Localization.getString("MESSAGE"), Core.Localization.getString("INTERNET_CONNECTION_ERROR"), ["Ok"], this);
	},

	restartGame:function()
	 {
		//console.log(">> Please restart! Your gamestate on server has been updated from other place.");
         var testUIAlert = new GAUIAlertView(Core.Localization.getString("warning"),Core.Localization.getString("MM_RESTART_GAME"),this,null,Core.Localization.getString("OK"));
		 testUIAlert.setType("ok");
		 testUIAlert.show();
	 },
	
	showProgress :function(progressStatus) {
		GameCentralHandler.showProgress(progressStatus);
		//console.log("===?????????--- delegate::showProgress() ---??????????==="+ progressStatus);
		
	},
	
	clickedButtonAtIndex: function(buttonIndex,alerWindow)
    {
    	
    	if (buttonIndex == 0) 
    	{
        	//NgLogE("OK button clicked");
		    if(alerWindow.getTagValue() == 1111)
		    {
		       this.startLoading();
			}									 
        } 
        else if (buttonIndex == 1)
        {
        	//NgLogE("Cancel button clicked");
    	}
    	
    },
	
	destroy: function() {
		
	},
	
});
